// Displays login and builds desktop
var sendlogin = function()
{
    var uname = $('uname').get('value');
    var pws = MD5($('pw').get('value'));
    var rstring = "do=login&user=" + uname + "&pw=" + pws;
    var a = new Request({url: 'index.php', method: 'post', data: rstring});
    a.addEvent('success', function(response){
        var user = JSON.decode(response);
        if(user.success == 1)
        {
            // Anmeldeformular löschen
            $('loginbox').destroy();
            builddesktop();
        } else
        {
            alert(user.success);
        }
    });
    a.send();
}

var user = {
    name : "",
    rights: "",
    loaddata: function() {
        var rstring = "do=getuserdata";
        var a = new Request({url: 'index.php', method: 'post', data: rstring, async: false});
        a.addEvent('success', function(response){
            var ud = JSON.decode(response);
            if(ud.name != "")
            {
                user.name = ud.username;
                user.rights = ud.rights;
            }
        });
        a.send();
    }
};

var builddesktop = function()
{
    user.loaddata();
    document.title = "apuxin - " + user.name;
    var splashscreen = new Element("p", {
        'id' : 'splashscreen'
    });

    $$('body').adopt(splashscreen);
    
    var statustext = new Element("h1", {'id': 'statustext'});
    splashscreen.adopt(statustext);
    // Hauptbibliothek laden...
    statustext.set('html', 'Hauptbibliothek laden...');
    new Asset.javascript('js/main.js', {id: 'mainjs'});
    statustext.set('html', 'Arbeitsfläche laden...');
    new Asset.javascript('js/workplace.js', {id: 'workplacejs'});
    statustext.set('html', 'Appmanager laden...');
    new Asset.javascript('js/appmanager.js', {id: 'appmanagerjs'});
    //statustext.set('html', 'Preapps starten...');
    splashscreen.destroy();
    $('boot').destroy();
}

var checkloggedin = function()
{
    var ans = false;
    var req = new Request({url: 'index.php', method: 'post', data: 'do=isonline', async: false});
    req.addEvent('success', function(response) {
        if(response == 1)
        {
            ans = true;
        } else
        {
            ans = false;
        }
        //alert(response);
    });
    req.send();
    return ans;
}
window.addEvent('domready', function() {
    if(checkloggedin() == true)
    {
        builddesktop();
    } else
    {
    // MD5-Bibliothek laden
    new Asset.javascript('js/md5.js', {id: 'md5'});
    var lbox = new Element("p", {
        'id' : 'loginbox'
    });
    var form = new Element("form", {
        'id' : 'loginform'
    });
    var uname = new Element("input", {
        'type' : 'text',
        'value': 'Username...',
        'id'   : 'uname'
    });
    uname.standard("", {color: '#000000'}, {color: '#C0C0C0'});
    var pw = new Element("input", {
        'type' : 'password',
        'value': 'Password',
        'id'   : 'pw'
    });
    pw.addEvent('keyenter', function(e) {
        e.stop();
        sendlogin();
    });
    pw.standard("", {color: '#000000'}, {color: '#C0C0C0'});
    var send = new Element("button", {
        events: {
            click: sendlogin
        },
        'type' : 'button',
        'id'   : 'send'
    });
    var logo = new Asset.image('themes/standard/apxlogo_small.png', {id: 'apxlogo', title: 'apuxin'});
    var title = new Element("h3");
    title.appendText("Login");
    send.appendText("Login");
    form.adopt(uname);
    form.adopt(pw);
    form.adopt(send);
    lbox.adopt(title);
    lbox.adopt(form);
    lbox.adopt(logo);
    $$('body').adopt(lbox);
    }
});

Element.Events.keyenter = {
	base: 'keyup',
	condition: function(e){
		return e.key=='enter';
	}
};

Element.implement({
  standard: function(text, writestyle, standardstyle) {
    if(!$chk(text)) {text = this.value;}
    this.addEvents({
      focus: function() {
          if(this.value == text)
          {
              this.value = "";
          }
          this.morph(writestyle);
      },
      blur: function() {
          if(this.value == "") {
              this.value = text;
          }
          this.morph(standardstyle);
      }
    });
  }
});